package v1

import (
	"net/http"
	"strconv"
	"time"

	"gitee.com/daonna/collab-manage-system/service"
	"github.com/gin-gonic/gin"
)

// CheckInRequest 上班打卡请求
type CheckInRequest struct {
	Remarks string `json:"remarks"` // 可选备注
}

// CheckOutRequest 下班打卡请求
type CheckOutRequest struct {
	Remarks string `json:"remarks"` // 可选备注
}

// 上班打卡
func CheckIn(c *gin.Context) {
	// 获取当前用户
	userID := c.GetUint("user_id")


	var req CheckInRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "无效的请求参数",
		})
		return
	}

	// 调用服务层
	serviceReq := service.CheckInOutRequest{
		Remarks: req.Remarks,
	}

	record, err := service.CheckIn(c.Request.Context(), userID, serviceReq)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "打卡失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "打卡成功",
		"data": map[string]interface{}{
			"record": record,
		},
	})
}

// 下班打卡
func CheckOut(c *gin.Context) {
	// 获取当前用户
	userID := c.GetUint("user_id")


	var req CheckOutRequest
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{
			"code":    400,
			"message": "无效的请求参数",
		})
		return
	}

	// 调用服务层
	serviceReq := service.CheckInOutRequest{
		Remarks: req.Remarks,
	}

	
	record, err := service.CheckOut(c.Request.Context(), userID, serviceReq)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "打卡失败: " + err.Error(),
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "打卡成功",
		"data": map[string]interface{}{
			"record": record,
		},
	})
}

// 获取今日打卡状态
func GetTodayAttendance(c *gin.Context) {
	// 获取当前用户
	userID := c.GetUint("user_id")

	// 调用服务层
	record, hasRecord, err := service.GetTodayAttendance(c.Request.Context(), userID)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "查询打卡记录失败",
		})
		return
	}

	if !hasRecord {
		c.JSON(http.StatusOK, gin.H{
			"code":    200,
			"message": "今日尚未打卡",
			"data": map[string]interface{}{
				"has_record": false,
				"record":     nil,
			},
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "获取成功",
		"data": map[string]interface{}{
			"has_record": true,
			"record":     record,
		},
	})
}

// 获取打卡历史记录
func GetAttendanceHistory(c *gin.Context) {
	// 获取当前用户
	userID := c.GetUint("user_id")


	// 获取查询参数
	yearStr := c.Query("year")
	monthStr := c.Query("month")

	var year, month int

	// 若未指定，使用当前年月
	now := time.Now()
	if yearStr == "" {
		year = now.Year()
	} else {
		year, _ = strconv.Atoi(yearStr)
	}

	if monthStr == "" {
		month = int(now.Month())
	} else {
		month, _ = strconv.Atoi(monthStr)
	}

	// 调用服务层
	records, err := service.GetAttendanceHistory(c.Request.Context(), userID, year, month)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "查询打卡记录失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "获取成功",
		"data": map[string]interface{}{
			"records": records,
			"total":   len(records),
		},
	})
}

// 获取月度考勤统计
func GetMonthlyAttendanceStats(c *gin.Context) {
	userID := c.GetUint("user_id")


	// 获取查询参数
	yearStr := c.Query("year")
	monthStr := c.Query("month")

	var year, month int

	// 若未指定，使用当前年月
	now := time.Now()
	if yearStr == "" {
		year = now.Year()
	} else {
		year, _ = strconv.Atoi(yearStr)
	}

	if monthStr == "" {
		month = int(now.Month())
	} else {
		month, _ = strconv.Atoi(monthStr)
	}

	// 调用服务层
	stats, err := service.GetMonthlyAttendanceStats(c.Request.Context(), userID, year, month)
	if err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{
			"code":    500,
			"message": "查询考勤统计失败",
		})
		return
	}

	c.JSON(http.StatusOK, gin.H{
		"code":    200,
		"message": "获取成功",
		"data": map[string]interface{}{
			"stats": stats,
		},
	})
}
